<template>
    <div class="global">
        <div class="form">
            <el-select v-model="fileType" style="width: 200px;margin-right:10px" placeholder="please select your zone">
                <el-option label="全部" :value="0" />
                <el-option label="文档" :value="1" />
                <el-option label="图片" :value="2" />
           
            </el-select>
        <el-button type="primary" @click="chaxun">查询</el-button>
        </div>
        <div class="form">
            <el-button type="blain" @click="shangchuan()">上传文件</el-button>
   
        </div>
        <div class="list">
            <el-table :data="list" style="width: 100%" border>
                <el-table-column prop="id" label="ID" width="80"></el-table-column>
                <el-table-column prop="name" label="文件名"></el-table-column>
                <el-table-column prop="url" label="文件链接">
                    <template #default="scope">

                        <div style="cursor: pointer;"  @click="download(scope.row.url)">
                            {{scope.row.name}}
                        </div>
                    </template>
                </el-table-column>
               
              
                <el-table-column label="操作" width="200">
                    <template #default="scope">
                        <el-button type="danger" size="small" @click="deleteWord(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
            :page-size="pageSize" background layout="total, sizes, prev, pager, next" :total="total"></el-pagination>

        <el-dialog :title="dialogTitle" v-model="dialogVisible" width="30%">
            <el-form :model="form" :rules="rules" ref="formRef" label-width="80px">
                <el-form-item label="文件类型" prop="fileType">
                    <el-select v-model="form.type" style="width: 100%;" placeholder="请选择类型">

                        <el-option label="文档" :value="1" />
                        <el-option label="图片" :value="2" />

                    </el-select>

                </el-form-item>
                <el-form-item label="文件名称" prop="name">
                    <el-input v-model="form.name" placeholder="请输入文件名称"></el-input>
                </el-form-item>
                <el-form-item label="文件路径" prop="phone">

                    <el-upload v-model:file-list="fileList" class="upload-demo" action="/api/oss/aliyun/upload" multiple
                        :on-remove="handleRemove" :limit="1" :headers="{
                            token: token
                        }" :on-success="handleSuccess" :on-exceed="handleExceed">
                        <el-button type="primary">请上传</el-button>

                    </el-upload>
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="dialogVisible = false">取消</el-button>
                    <el-button type="primary" @click="submitForm">确定</el-button>
                </span>
            </template>
        </el-dialog>

    </div>
</template>
<script setup>
import service from '@/utils/request';
import { ElMessageBox } from 'element-plus';

import { onMounted, ref } from 'vue';
const fileType = ref(1);
const list = ref([]);
const token = ref("");
onMounted(() => {
    getlist()
    if (window.localStorage.getItem("userinfo")) {
        token.value = (JSON.parse(window.localStorage.getItem("userinfo"))).token

    }
})
const fileList = ref([]);
const currentPage = ref(1)
const total = ref(0)
const pageSize = ref(10)
const form = ref({
    name: "",
    type: "",
    url: ""
})
const dialogVisible = ref(false);
const dialogTitle = ref("新增文件");
const download=(e)=>{
    console.log(e)
    // window.location.href=e
    // window.open(e,'_blank')
   
    const a = document.createElement("a");
a.href=e
a.download = e
a.click();

    
}
const handleSizeChange = (e) => {
    console.log(e)
    pageSize.value = e
    getlist()

}
const deleteWord = (row) => {
    ElMessageBox.confirm(`确定要删除该文件  吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
    }).then(async() => {
       const res=await service.delete(`/api/sys/file/${row.id}`)
  if(res.code == 200){
    getlist()
  }
    }).catch(() => {
        ElMessage.info('已取消删除');
    });
};

const handleCurrentChange = (e) => {
    currentPage.value = e;
    getlist()
}
const shangchuan = () => {
    dialogTitle.value = "新增文件"
    dialogVisible.value = true
}
const handleSuccess = (e) => {
    console.log(e)
    form.value.url = e.data
    // fileList.value.push({
    //     name:"文件名",
    //     url:e.data
    // })
}
const chaxun=()=>{
    currentPage.value=1;
    pageSize.value=10
    getlist()
}
const getlist = async () => {
    const res = await service.get("/api/sys/file/list", {
        params: {
            type:fileType.value == 0 ? '' :fileType.value,

            current: currentPage.value,
            size: pageSize.value
        }
    })
    console.log(res)
    list.value = res.data.records
    total.value = res.data.total

}
const submitForm = async () => {
    if (dialogTitle.value == '新增文件') {
        const res = await service.post("/api/sys/file", {
            name: form.value.name,
            url: form.value.url,
            type:String(form.value.type) 

        })
        console.log(res)
        if (res.code == 200) {
            dialogVisible.value=false
            getlist()
        }
    }

}
</script>
<style scoped>
.list {
    padding: 10px;
    box-sizing: border-box;

}

.form {
    padding: 10px;
    width: 100%;
}

.global {
    width: 100%;
    height: 100%;
    background: #fff;
}
</style>